package cn.codesea.housework_user.ui.worker;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import com.google.gson.Gson;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.entity.Worker;
import cn.codesea.housework_user.ui.BaseActivity;
import cn.codesea.housework_user.ui.RegisterChildActivity;
import cn.codesea.housework_user.utils.SPUtil;

public class RegisterWorkerActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.pwd_et)
    EditText pwd_et;
    @BindView(R.id.name_et)
    EditText name_et;
    @BindView(R.id.sex_et)
    EditText sex_et;
    @BindView(R.id.cif_et)
    EditText cif_et;
    @BindView(R.id.phone_et)
    EditText phone_et;
    @BindView(R.id.address_et)
    EditText address_et;
    @BindView(R.id.birth_et)
    EditText birth_et;

    @BindView(R.id.register_tv)
    AppCompatButton register_tv;

    Worker worker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_worker);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData(){
        Intent data = getIntent();
        String type = data.getStringExtra("type");
        if("update".equals(type)){
            worker = SPUtil.getWorker(RegisterWorkerActivity.this);
        }
    }

    public void initToolbar(){
        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initView(){
        initToolbar();

        if(worker != null){
            pwd_et.setText(worker.getPwd());
            name_et.setText(worker.getName());
            sex_et.setText(worker.getSex());
            cif_et.setText(worker.getCif_no());
            phone_et.setText(worker.getPhone());
            birth_et.setText(worker.getBirth());
            address_et.setText(worker.getAddress());
        }
    }

    @OnClick({R.id.sex_et})
    public void sex_et(){
        showDialog_sex(sex_et);
    }

    @OnClick({R.id.birth_et})
    public void birth_et(){
        showDialog_birth(birth_et);
    }

    @OnClick(R.id.register_tv)
    public void register_tv(){

        Worker worker = new Worker();

        String type = "register";
        if (this.worker != null){
            type = "update";
            worker.setWorkerid(this.worker.getWorkerid());
        }
        worker.setPwd(pwd_et.getText().toString());
        worker.setName(name_et.getText().toString());
        worker.setSex(sex_et.getText().toString());
        worker.setCif_no(cif_et.getText().toString());
        worker.setPhone(phone_et.getText().toString());
        worker.setBirth(birth_et.getText().toString());
        worker.setAddress(address_et.getText().toString());
        startActivity(new Intent(RegisterWorkerActivity.this,ServiceListWorkerActivity.class).putExtra("worker",worker).putExtra("type",type));
    }

    /**
     * 性别选择
     * @param et
     */
    public void showDialog_sex(final EditText et){
        final String[] Items = new String[] {"男","女"};
        Dialog allertDialog = new AlertDialog.Builder(RegisterWorkerActivity.this).setItems(Items,new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                et.setText(Items[which]);
            }
        } ).create();
        allertDialog.show();
    }

    /**
     * 日期选择
     * @param et
     */
    public void showDialog_birth(final EditText et){
        new DatePickerDialog(RegisterWorkerActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        monthOfYear += 1;
                        et.setText(year+"-"+monthOfYear+"-"+dayOfMonth);
                    }
                }, Calendar.getInstance().get(Calendar.YEAR), Calendar
                .getInstance().get(Calendar.MONTH), Calendar
                .getInstance().get(Calendar.DATE)).show();
    }


}
